package cn.edu.pku.ss.situationawareperceiver;

import java.util.ArrayList;
import java.util.List;

import org.alljoyn.bus.Status;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class UIHandler extends Handler {

	private static final String TAG = "UIHandler";

	private MainActivity mActivity;

	/* Define UIHandler codes here */
	public static final int TOAST_MSG = 0;
	public static final int UPDATE_DISCOVERING_SPINNER = 2;
	public static final int UPDATE_CONNECTION_STATUS = 3;
	public static final int UPDATE_GROUP_STATUS = 4;
	public static final int UPDATE_PEERS_LIST = 5;

	public static final int ACTIVE_TAG = 6;
	public static final int UNACTIVE_TAG = 7;
	public static final int SHOW_ABDUCTIVE_PROCESS = 8;

	public static final int CLEAR_AND_REGENERATE_REQUEST = 9;

	public UIHandler(MainActivity activity) {
		mActivity = activity;
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case TOAST_MSG: {
			Toast.makeText(mActivity.getApplicationContext(), (String) msg.obj,
					Toast.LENGTH_LONG).show();
			break;
		}
		case UPDATE_CONNECTION_STATUS: {
			mActivity.connectStatus_TextView.setText("ConnectionStatus: "
					+ (String) msg.obj);
			break;
		}
		case UPDATE_GROUP_STATUS: {
			mActivity.groupStatus_TextView.setText((String) msg.obj);
			break;
		}
		case UPDATE_PEERS_LIST: {
			ArrayList<String> peersList = (ArrayList<String>) msg.obj;
			int size = peersList.size();
			if (0 != size) {
				int count = 1;
				String peersListStr = "Reachable Peers -> ";
				for (String peername : peersList) {
					peersListStr += "(" + count++ + ")" + peername + " ";
				}
				mActivity.peerslist_TextView.setText(peersListStr);
			} else {
				mActivity.peerslist_TextView.setText("No peer is reachable.");
			}
			break;
		}
		case ACTIVE_TAG: {
			int tagId = (Integer) msg.obj;
			mActivity.activateTag(tagId);
			break;
		}
		case UNACTIVE_TAG: {
			int tagId = (Integer) msg.obj;
			mActivity.unactivateTag(tagId);
			break;
		}
		case SHOW_ABDUCTIVE_PROCESS: {

			List<Integer> situationIds = (List<Integer>) msg.obj;
			logInfo("Handling message of SHOW_ABDUCTIVE_PROCESS. situation ids size = "
					+ situationIds.size());
			mActivity.showProcess(situationIds);
			break;
		}
		case CLEAR_AND_REGENERATE_REQUEST: {
			if (!mActivity.isTimerOn())
				break;
			mActivity.clearAndGenerateContextShareRequest();
			Message toastMsg = this.obtainMessage(TOAST_MSG,
					"Clear and regenerate a request.");
			this.sendMessage(toastMsg);
			break;
		}
		default:
			break;
		}
	}

	/* Log methods */
	public void logInfo(String msg) {
		Log.i(TAG, msg);
	}

	public void logStatus(String msg, Status status) {
		String log = String.format("%s: %s", msg, status);
		if (status == Status.OK) {
			Log.i(TAG, log);
		} else {
			Message toastMsg = obtainMessage(TOAST_MSG, log);
			sendMessage(toastMsg);
			Log.e(TAG, log);
		}
	}

	public void logError(String msg) {
		Message toastMsg = obtainMessage(TOAST_MSG, msg);
		sendMessage(toastMsg);
		Log.e(TAG, msg);
	}

	public void logException(String msg, Exception ex) {
		String log = String.format("%s: %s", msg, ex);
		Message toastMsg = obtainMessage(TOAST_MSG, log);
		sendMessage(toastMsg);
		Log.e(TAG, log, ex);
	}
}
